package com.zt.rongda.stock.service;

import java.util.List;

import com.zt.rongda.stock.inter.StockPurchaseDetailMapper;
import com.zt.rongda.stock.model.StockPurchaseDetail;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockPurchaseDetailService extends BaseService {

    @Autowired
    private StockPurchaseDetailMapper stockPurchaseDetailMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from STOCK_PURCHASE_DETAIL where 1=1 ");
        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 根据单号查询
     *
     * @param reqBean
     * @return
     */
    public JSONArray queryByListId(ReqBean reqBean) {
        String sql = "select * from STOCK_PURCHASE_DETAIL where LIST_ID = ?" ;
        return this.query(sql, new Object[]{reqBean.getInfo("listId")});
    }

    /**
    * 通过id查询
    *
    * @param id
    * @return
    */
    public StockPurchaseDetail getById(String id) {
        return stockPurchaseDetailMapper.selectByPrimaryKey(id);
    }

    /**
    * 新增
    *
    * @param stockPurchaseDetail
    */
    public void add(StockPurchaseDetail stockPurchaseDetail) {
        stockPurchaseDetail.setId(stringId());// 手动设置Id

        stockPurchaseDetail.setMakeTime(new Date());
        stockPurchaseDetail.setModifyTime(stockPurchaseDetail.getMakeTime());
        stockPurchaseDetailMapper.insert(stockPurchaseDetail);
    }

    /**
    * 修改
    *
    * @param stockPurchaseDetail
    */
    public void update(StockPurchaseDetail stockPurchaseDetail) {
        String id = stockPurchaseDetail.getId();
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }

        stockPurchaseDetail.setModifyTime(new Date());
        stockPurchaseDetailMapper.updateByPrimaryKeySelective(stockPurchaseDetail);
    }

    /**
    * 删除
    *
    * @param id
    */
    public void delete(String id) {
        if (StringUtil.isEmpty(id)) {
            throw new BusinessException("获取主键失败");
        }
        stockPurchaseDetailMapper.deleteByPrimaryKey(id);
    }

}